package com.zidian.study.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.zidian.study.activity.EvaMoreActivity;
import com.zidian.study.activity.EvaluateTagActivity;
import com.zidian.study.bean.EvaluateTeacherBean;

import java.util.List;

import com.zidian.student.trucks.R;

/**
 * Created by Administrator on 2016/8/24 0024.
 * 评价列表适配器
 */
public class EvaluateAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {


    private static final int TYPE_ITEM = 0;
    private static final int TYPE_FOOTER = 1;
    private Context context;
    private List<EvaluateTeacherBean.DataBean> data;
    private static boolean hasMore = true;


    public EvaluateAdapter(Context context, List<EvaluateTeacherBean.DataBean> data) {
        this.context = context;
        this.data = data;
    }

    public void setHasMore(boolean hasMore) {
        this.hasMore = hasMore;
    }

    public interface OnItemClickListener {
        void onItemClick(View view, int position);

        void onItemLongClick(View view, int position);
    }

    private OnItemClickListener onItemClickListener;

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    @Override
    public int getItemCount() {
        return data.size() == 0 ? 0 : data.size() + 1;
    }

    @Override
    public int getItemViewType(int position) {
        if (position + 1 == getItemCount()) {
            return TYPE_FOOTER;
        } else {
            return TYPE_ITEM;
        }
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == TYPE_ITEM) {
            View view = LayoutInflater.from(context).inflate(R.layout.item_evaluate, parent,
                    false);
            return new ItemViewHolder(view);
        } else if (viewType == TYPE_FOOTER) {
            View view = LayoutInflater.from(context).inflate(R.layout.item_interaction_ask_foot, parent,
                    false);
            return new FootViewHolder(view);
        }
        return null;
    }


    @Override
    public void onBindViewHolder(final RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof ItemViewHolder) {

            ((ItemViewHolder) holder).evaluateName.setText(data.get(position).getTeacherName());
            ((ItemViewHolder) holder).evaluateClass.setText(data.get(position).getCourseName());
            ((ItemViewHolder) holder).evaluateTagName.setText(data.get(position).getTwoIndexName());

            Glide.with(context)
                    .load(data.get(position).getHeadPortrait())
                    .placeholder(R.drawable.teacher)
                    .centerCrop()
                    .into(((ItemViewHolder) holder).evaluateImage);

            ((ItemViewHolder) holder).tagEvaluate.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(context, EvaluateTagActivity.class);
                    intent.putExtra("teacherId", data.get(holder.getAdapterPosition()).getTeacherId());
                    intent.putExtra("twoIndexName", data.get(holder.getAdapterPosition()).getTwoIndexName());
                    intent.putExtra("pushId", data.get(holder.getAdapterPosition()).getPushId());
                    intent.putExtra("headPortrait", data.get(holder.getAdapterPosition()).getHeadPortrait());
                    intent.putExtra("pushTime", data.get(holder.getAdapterPosition()).getPushTime());
                    intent.putExtra("courseId", data.get(holder.getAdapterPosition()).getCourseId());
                    intent.putExtra("courseName", data.get(holder.getAdapterPosition()).getCourseName());
                    context.startActivity(intent);
                }
            });

            ((ItemViewHolder) holder).customEvaluate.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(context, EvaMoreActivity.class);

                    intent.putExtra("teacher", data.get(holder.getAdapterPosition()));

                    context.startActivity(intent);
                }
            });


            //holder.tv.setText(data.get(position));
            if (onItemClickListener != null) {
                holder.itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        int position = holder.getLayoutPosition();
                        onItemClickListener.onItemClick(holder.itemView, position);
                    }
                });

                holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
                    @Override
                    public boolean onLongClick(View v) {
                        int position = holder.getLayoutPosition();
                        onItemClickListener.onItemLongClick(holder.itemView, position);
                        return false;
                    }
                });
            }
        } else if (holder instanceof FootViewHolder) {
                ((FootViewHolder) holder).loadText.setText("全部数据加载完成!");
                ((FootViewHolder) holder).progressBar.setVisibility(View.GONE);

        }
    }


    static class ItemViewHolder extends RecyclerView.ViewHolder {

        TextView tagEvaluate;
        TextView customEvaluate;
        TextView evaluateName;
        TextView evaluateClass;
        ImageView evaluateImage;
        TextView evaluateTagName;

        public ItemViewHolder(View view) {
            super(view);
            tagEvaluate = (TextView) view.findViewById(R.id.evaluate_tag);
            customEvaluate = (TextView) view.findViewById(R.id.evaluate_custom);
            evaluateName = (TextView) view.findViewById(R.id.evaluate_list_name);
            evaluateClass = (TextView) view.findViewById(R.id.evaluate_list_class);
            evaluateImage = (ImageView) view.findViewById(R.id.evaluate_list_image);
            evaluateTagName = (TextView) view.findViewById(R.id.evaluate_tag_name);

        }
    }

    static class FootViewHolder extends RecyclerView.ViewHolder {

        TextView loadText;
        ProgressBar progressBar;

        public FootViewHolder(View view) {
            super(view);
            loadText = (TextView) view.findViewById(R.id.load_text);
            progressBar = (ProgressBar) view.findViewById(R.id.progressBar);
        }
    }
}
